home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / quodlibet / qltk / wlw.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  9KB  |  198 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. import math
  5. import time
  6. from gi.repository import Gtk, Pango, Gdk
  7. from quodlibet.qltk import get_top_parent
  8. from quodlibet import util
  9.  
  10. class WaitLoadBase(object):
  11.     '''Abstract class providing a label, a progressbar, pause/stop buttons,
  12.     and the stepping logic.'''
  13.     
  14.     def __init__(self, count = 0, text = '', initial = { }, limit = 3):
  15.         '''count: the total amount of items expected, 0 for unknown/indefinite
  16.         text: text to display in the label; may contain % formats
  17.         initial: initial values for % formats (text % initial)
  18.         limit: count must be greater than limit (or 0) for pause/stop to appear
  19.  
  20.         The current iteration of the counter can be gotten as
  21.         self.current. count can be gotten as self.count.
  22.         '''
  23.         super(WaitLoadBase, self).__init__()
  24.         self._label = Gtk.Label()
  25.         self._label.set_use_markup(True)
  26.         self._progress = Gtk.ProgressBar()
  27.         self._progress.set_pulse_step(0.08)
  28.         self.pulse = self._progress.pulse
  29.         self.set_fraction = self._progress.set_fraction
  30.         self.set_text = self._label.set_markup
  31.         self.setup(count, text, initial)
  32.         if self.count > limit or self.count == 0:
  33.             self._cancel_button = Gtk.Button.new_from_stock(Gtk.STOCK_STOP)
  34.             self._pause_button = Gtk.ToggleButton(label = Gtk.STOCK_MEDIA_PAUSE)
  35.             self._pause_button.set_use_stock(True)
  36.             self._cancel_button.connect('clicked', self._WaitLoadBase__cancel_clicked)
  37.             self._pause_button.connect('clicked', self._WaitLoadBase__pause_clicked)
  38.         else:
  39.             self._cancel_button = None
  40.             self._pause_button = None
  41.  
  42.     
  43.     def setup(self, count = 0, text = '', initial = { }):
  44.         self.current = 0
  45.         self.count = count
  46.         self._text = text
  47.         self.paused = False
  48.         self.quit = False
  49.         self._start_time = time.time()
  50.         initial.setdefault('total', self.count)
  51.         initial.setdefault('current', self.current)
  52.         initial.setdefault('remaining', _('Unknown'))
  53.         self._label.set_markup(self._text % initial)
  54.         self._progress.set_fraction(0)
  55.  
  56.     
  57.     def __pause_clicked(self, button):
  58.         self.paused = button.get_active()
  59.  
  60.     
  61.     def __cancel_clicked(self, button):
  62.         self.quit = True
  63.  
  64.     
  65.     def step(self, **values):
  66.         """Advance the counter by one. Arguments are applied to the
  67.         originally-supplied text as a format string.
  68.  
  69.         This function doesn't return if the dialog is paused (though
  70.         the GTK main loop will still run), and returns True if stop
  71.         was pressed.
  72.         """
  73.         values.setdefault('total', self.count)
  74.         values.setdefault('current', self.current)
  75.         if self.count:
  76.             t = (time.time() - self._start_time) / self.current
  77.             remaining = math.ceil((self.count - self.current) * t)
  78.             values.setdefault('remaining', util.format_time_display(remaining))
  79.         self._label.set_markup(self._text % values)
  80.         while not (self.quit):
  81.             if self.paused or Gtk.events_pending():
  82.                 Gtk.main_iteration()
  83.             return self.quit
  84.  
  85.  
  86.  
  87. class WaitLoadWindow(WaitLoadBase, Gtk.Window):
  88.     '''A window with a progress bar and some nice updating text,
  89.     as well as pause/stop buttons.
  90.  
  91.     Example:
  92.  
  93.     w = WaitLoadWindow(None, 5, "%(current)d/%(total)d")
  94.     for i in range(1, 6): w.step()
  95.     w.destroy()
  96.     '''
  97.     
  98.     def __init__(self, parent, *args):
  99.         '''parent: the parent window, or None'''
  100.         Gtk.Window.__init__(self, type = Gtk.WindowType.POPUP)
  101.         WaitLoadBase.__init__(self)
  102.         self.setup(*args)
  103.         parent = get_top_parent(parent)
  104.         if parent:
  105.             sig = parent.connect('configure-event', self._WaitLoadWindow__recenter)
  106.             self.connect('destroy', self._WaitLoadWindow__reset_cursor, parent)
  107.             self.connect('destroy', self._WaitLoadWindow__disconnect, sig, parent)
  108.             sig_vis = parent.connect('visibility-notify-event', self._WaitLoadWindow__update_visible)
  109.             self.connect('destroy', self._WaitLoadWindow__disconnect, sig_vis, parent)
  110.             self.set_transient_for(parent)
  111.             window = parent.get_window()
  112.             if window:
  113.                 window.set_cursor(Gdk.Cursor.new(Gdk.CursorType.WATCH))
  114.             
  115.         self.set_modal(True)
  116.         self.add(Gtk.Frame())
  117.         self.get_child().set_shadow_type(Gtk.ShadowType.OUT)
  118.         vbox = Gtk.VBox(spacing = 12)
  119.         vbox.set_border_width(12)
  120.         self._label.set_size_request(170, -1)
  121.         self._label.set_line_wrap(True)
  122.         self._label.set_justify(Gtk.Justification.CENTER)
  123.         vbox.pack_start(self._label, True, True, 0)
  124.         vbox.pack_start(self._progress, True, True, 0)
  125.         if self._cancel_button and self._pause_button:
  126.             hbox = Gtk.HBox(spacing = 6, homogeneous = True)
  127.             hbox.pack_start(self._cancel_button, True, True, 0)
  128.             hbox.pack_start(self._pause_button, True, True, 0)
  129.             vbox.pack_start(hbox, True, True, 0)
  130.         self.get_child().add(vbox)
  131.         self.set_position(Gtk.WindowPosition.CENTER_ON_PARENT)
  132.         self.get_child().show_all()
  133.         while Gtk.events_pending():
  134.             Gtk.main_iteration()
  135.  
  136.     
  137.     def __update_visible(self, parent, event):
  138.         if event.state == Gdk.VisibilityState.FULLY_OBSCURED:
  139.             self.hide()
  140.         else:
  141.             self.show()
  142.  
  143.     
  144.     def __recenter(self, parent, event):
  145.         (x, y) = parent.get_position()
  146.         (dx, dy) = parent.get_size()
  147.         (dx2, dy2) = self.get_size()
  148.         self.move(x + dx // 2 - dx2 // 2, y + dy // 2 - dy2 // 2)
  149.  
  150.     
  151.     def __disconnect(self, widget, sig, parent):
  152.         parent.disconnect(sig)
  153.  
  154.     
  155.     def __reset_cursor(self, widget, parent):
  156.         if parent.get_window():
  157.             parent.get_window().set_cursor(None)
  158.  
  159.  
  160.  
  161. class WritingWindow(WaitLoadWindow):
  162.     '''A WaitLoadWindow that defaults to text suitable for saving files.'''
  163.     
  164.     def __init__(self, parent, count):
  165.         super(WritingWindow, self).__init__(parent, count, _('Saving the songs you changed.') + '\n\n' + _('%(current)d/%(total)d songs saved\n(%(remaining)s remaining)'))
  166.  
  167.     
  168.     def step(self):
  169.         return super(WritingWindow, self).step()
  170.  
  171.  
  172.  
  173. class WaitLoadBar(WaitLoadBase, Gtk.HBox):
  174.     
  175.     def __init__(self):
  176.         super(WaitLoadBar, self).__init__()
  177.         self._label.set_alignment(0, 0.5)
  178.         self._label.set_ellipsize(Pango.EllipsizeMode.END)
  179.         self._cancel_button.remove(self._cancel_button.get_child())
  180.         self._cancel_button.add(Gtk.Image.new_from_stock(Gtk.STOCK_STOP, Gtk.IconSize.MENU))
  181.         self._pause_button.remove(self._pause_button.get_child())
  182.         self._pause_button.add(Gtk.Image.new_from_stock(Gtk.STOCK_MEDIA_PAUSE, Gtk.IconSize.MENU))
  183.         self.pack_start(self._label, True, True, 0)
  184.         self.pack_start(self._progress, False, True, 6)
  185.         self.pack_start(self._pause_button, False, True, 0)
  186.         self.pack_start(self._cancel_button, False, True, 0)
  187.         for child in self.get_children():
  188.             child.show_all()
  189.         
  190.  
  191.     
  192.     def step(self, **values):
  193.         ret = super(WaitLoadBar, self).step(**values)
  194.         self._progress.set_text(_('%d of %d') % (self.current, self.count))
  195.         return ret
  196.  
  197.  
  198.